﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainController.Hardware.PLC.MEGMEET.Communication
{
    public enum CmdTypeEnum
    {
        None,
        Read,
        WriteBytes,
        WriteBit
    }

    internal class CommandUnit
    {
        public CmdTypeEnum CmdType = CmdTypeEnum.None;

        public string IDCType = "D";
        public ushort Addr = 0;
        public Byte SubAddr = 0;
        public List<ushort> DataList = new List<ushort>();
        public bool BitValue = false;

        public CommandUnit(ushort addr, List<byte> dataList, string idcType)
        {
            Addr = addr;

            for (int i = 0; i < dataList.Count; i=i+2)
            {
                ushort data = (ushort)(dataList[i] << 8 | dataList[i + 1]);
                DataList.Add(data);
            }

            CmdType = CmdTypeEnum.WriteBytes;

            IDCType = idcType;
        }

        public CommandUnit(ushort addr, byte subAddr, byte[] dataList, bool val, string idcType)
        {
            Addr = addr;
            SubAddr = subAddr;
            ushort data = (ushort)(dataList[0] << 8 | dataList[0 + 1]);
            DataList.Add(data);
            BitValue = val;
            CmdType = CmdTypeEnum.WriteBit;

            IDCType = idcType;
        }
    }
}
